package com.imooc.girl.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by 10077 on 2018/3/14.
 * desc:
 */
@Aspect
@Component
@Slf4j
public class HttpAspect {

//    @Before("execution(public * com.imooc.girl.controller.GirlController.girlList(..))")
    @Pointcut("execution(public * com.imooc.girl.controller.GirlController*(..))")
    public void log(){
    }

    @Before("log()")
    public void doBefore(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

//        获取url
        log.info("url{}",request.getRequestURL());
//        获取方法
        log.info("method{}",request.getMethod());
//        获取ip
        log.info("ip{}",request.getRemoteAddr());
//        获取类方法
        log.info("class_method");
//        参数
        log.info("args{}");
        System.out.println(11111111);
    }

    @After("log()")
    public void doAfter(){
        log.info("");
        System.out.println(2222222);
    }

    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturnning(Object object){
        log.info("response{}",object);
    }
}
